package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dto.*;
import com.cskaoyan.user.form.UpdateAddressRequest;
import com.cskaoyan.user.service.IAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shopping")
public class AddressController {

    @Autowired
    IAddressService addressService;

    /**
     * 获取当前用户的地址列表
     *
     * @return
     */
    @GetMapping("/addresses")
    public ResponseData addressList(HttpServletRequest request) {
//        String userInfo = request.getHeader("user_info");
//        JSONObject object = JSON.parseObject(userInfo);
//        Long uid = Long.parseLong(object.get("id").toString());
//        Cookie[] cookies = request.getCookies();
//        String value = null;
//        for (Cookie cookie : cookies) {
//            if (cookie.getName().equals("access_token")){
//                value = cookie.getValue();
//            }
//        }
//        JwtTokenUtils build = JwtTokenUtils.builder().token(value).build();
//        String s = build.freeJwt();
//        Member member = JSON.parseObject(s, Member.class);
//        Long uid = member.getId();

        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("id").toString());
        List<AddressDto> addressDtos = addressService.addressList(uid);
        return new ResponseUtil().setData(addressDtos);
    }

    @PostMapping("/addresses")
    public ResponseData addressAdd(@Validated @RequestBody AddAddressRequest request,
                                   HttpServletRequest servletRequest) {
        log.debug(request.getIsDefault()+"");
        log.debug(request.toString());

        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = object.getLong("id");
        request.setUserId(uid);
        addressService.createAddress(request);
        return new ResponseUtil().setData(null);
    }

    @DeleteMapping("/addresses/{addressid}")
    public ResponseData addressDel(@PathVariable("addressid") @NotNull(message = "adrdessId不能为null") Long addressid) {
        addressService.deleteAddress(addressid);
        return new ResponseUtil().setData(null);

    }

    @PutMapping("/addresses")
    public ResponseData addressUpdate(@RequestBody UpdateAddressRequest request,
                                      HttpServletRequest servletRequest) {
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("id").toString());
        request.setUserId(uid);
        addressService.updateAddress(request);
        return new ResponseUtil().setData(null);
    }
}
